<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 云哥
  Date: 2023/5/30
  Time: 10:04
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>通过连接池访问数据库</title>
</head>
<body>
<h1>通过连接池访问数据库</h1>
    <%
        Context context=null;

        context=new InitialContext();

        DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/bookshopplus");

        Connection connection = ds.getConnection();

        Statement statement = connection.createStatement();

        ResultSet rs = statement.executeQuery("select id,title from books limit 10");

        while (rs.next()) {
            int id = rs.getInt("id");
            String title = rs.getString("title");
            out.print("<p><a href='details.jsp?id="+id+"'>"+title+"</a></p>");
        }

        rs.close();
        statement.close();
        connection.close();//不是关闭,而是归还给——>连接池

        context.close();
    %>
</body>
</html>
